home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
pascal
/
spoc88.zip
/
DCG.ZIP
/
DIFFRENC.PRO
< prev
next >
Wrap
Text File
|
1988-06-14
|
3KB
|
109 lines
/* Parsing by difference lists
Barbara Clinger, 1988
For input and output, this program is identical to
Program Listing 1. However, the expansion of the grammar is
done with difference lists rather than using append to split
the list of tokens into noun phrases and verb phrases.
*/
domains
toklist = string*
predicates
do
reader(string,toklist) /* the reader */
remove_period(toklist,toklist)
append(toklist,toklist,toklist)
/* The grammar */
sentence(toklist,toklist)
noun_phrase(toklist,toklist)
verb_phrase(toklist,toklist)
determiner(toklist,toklist)
noun(toklist,toklist)
verb(toklist,toklist)
goal
do.
clauses
do :-
nl,write("Enter a sentence --> "),
readln(S),nl,nl,
reader(S,List),
write("Output of the reader: ",List),nl,nl,
remove_period(List,List_in),
sentence(List_in,List_out),
write("List out: ",List_out),nl, /* informational write */
List_out = [].
/* do succeeds when List_out = [], that is, all the list was parsed */
/* sentence:
List_in is a sentence if List_in - Y is a noun phrase and
Y - Rest is a verb phrase. If the predicate sentence succeeds
in parsing the entire list then Rest is the empty list.
*/
sentence(List_in,Rest) :-
noun_phrase(List_in,Y),verb_phrase(Y,Rest).
/* noun_phrase
X is a noun phrase
if X - Y is a determiner and Y - Rest is a noun,
or if X - Y is a noun.
*/
noun_phrase(X,Rest) :- determiner(X,Y),noun(Y,Rest).
noun_phrase(X,Y) :- noun(X,Y).
/* verb_phrase
X is a verb phrase
if X - Y is a verb and Y - Rest is a noun phrase,
or if X - Y is a verb and Y - Rest is a noun
or if X - Y is a verb.
*/
verb_phrase(X,Rest) :- verb(X,Y), noun_phrase(Y,Rest).
verb_phrase(X,Rest) :- verb(X,Y), noun(Y,Rest).
verb_phrase(X,Rest) :- verb(X,Rest).
/* the dictionary
Since the predicate determiner is called with determiner(X,Y),
where X is ["the"|Rest], determiner is saying is that
"the" is a determiner since ["the"] is ["the"|Rest] - Rest.
*/
determiner(["the"|Rest],Rest).
determiner([ "a" |Rest],Rest).
noun(["man" |Rest],Rest).
noun(["john"|Rest],Rest).
noun(["mary"|Rest],Rest).
noun(["dog" |Rest],Rest).
verb(["likes"|Rest],Rest).
verb(["sees"|Rest],Rest).
/* end of dictionary */
/* reader
(1) the empty string returns the empty list,
(2) if the string is not empty, recursively it takes the front token,
converts it to lower case, then reads the rest of the list,
until the string is empty.
*/
reader("",[]) :- !.
reader(Str,[Token|Rest]) :-
fronttoken(Str,Tok,Str1),
upper_lower(Tok,Token),
reader(Str1,Rest),!.
/* remove a period at the end of a sentence */
remove_period(L1,L2) :-
append(L2,["."],L1).
remove_period(L1,L1).
append([],List,List).
append([H|T],L,[H|T2]) :-
append(T,L,T2).